package com.dream.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Component;

import com.dream.common.Common;
import com.dream.common.SuperDao;
import com.dream.model.SysUser;
import com.dream.model.Sys_Lable;
import com.dream.system.service.ISysLable;

@Component("sysLableService")
@SuppressWarnings("all")
public class SysLableService extends SuperDao<Object> implements ISysLable {

	@SuppressWarnings("all")
	@Override
	public int addSysLable(Sys_Lable model) {

		String hql = "from Sys_Lable t where t.name = ?";
		Object[] objec = new Object[] { model.getName() };
		List<Sys_Lable> sllist = this.getList(hql, objec, 0, 0, "");
		if (null == sllist || sllist.size() <= 0) {
			model.setLableId(UUID.randomUUID().toString());
			model.setCreateTime(Common.getTime1());
			model.setShowTime(Common.getTime());
			this.addT(model);
			return 1;
		} else {
			return 2;
		}
	}

	/**
	 * 查询所有标签
	 */
	@SuppressWarnings("all")
	@Override
	public Map getAllLable(Sys_Lable model) {
		// TODO Auto-generated method stub
		// String opt =
		// "t.table_id, t.name, t.lableImportance, t.lableValue, t.type, t.jiaocai, t.lableId, t.lablePid, t.userName";
		// 这里把用户名放在string1里面的
		String opt = " t.table_id, t.name, t.lableImportance, t.lableValue, t.type, t.jiaocai, t.lableId, t.lablePid, t.sysUser.userName,t.sysUser.table_id,t.showTime ";
		String hsql = "from Sys_Lable t ";
		String wheresql = "";
		if (null != model.getType() && !"".equals(model.getType())) {
			if (wheresql.length() > 3) {
				wheresql += " and t.type ='" + model.getType() + "' ";
			} else {
				wheresql += " where t.type ='" + model.getType() + "' ";
			}
		}
		if (model != null && null != model.getName() && !"".equals(model.getName())) {
			if (wheresql.length() > 3) {
				hsql += " and t.name like '%" + model.getName() + "%'";
			} else {
				hsql += " where t.name like '%" + model.getName() + "%'";
			}
		}
		Object[] obj = new Object[] { model.getTable_id() };
		@SuppressWarnings("unchecked")
		List<SysUser> list1 = (List<SysUser>) this.getList("select new com.dream.model.Sys_Lable(" + opt + ") " + hsql, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + hsql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	/**
	 * 删除一个标签
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int deleteLable(Sys_Lable model) {

		Sys_Lable sl = (Sys_Lable) this.getT(new Sys_Lable(), model.getTable_id());

		String parentId = sl.getLablePid();

		if (null != parentId && !"0".equals(parentId)) {
			String str = "from Sys_Lable t where t.lableId = ? ";
			Object obj[] = new Object[] { parentId };
			// 查询自己的上级，
			List<Sys_Lable> plist = this.getList(str, obj, 0, 0, "");
			if (null != plist && plist.size() > 0) {
				// 查自己的下级
				String str1 = "from Sys_Lable t where t.lablePid = ? ";
				Object[] object = new Object[] { sl.getLableId() };
				List<Sys_Lable> chilrenlist = this.getList(str1, object, 0, 0, "");
				if (null != chilrenlist) {
					for (int i = 0; i < chilrenlist.size(); i++) {
						Sys_Lable slc = chilrenlist.get(i);
						slc.setLablePid(plist.get(0).getLableId());
					}
				}
			}
		}
		this.delete(sl);
		// TODO Auto-generated method stub
		return 1;
	}

	/**
	 * 更新一个标签
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int updateSysLable(Sys_Lable model) {
		// TODO Auto-generated method stub
		if (model == null || null == model.getTable_id() || "".equals(model.getTable_id())) {
			return 0;
		}
		String hql = "from Sys_Lable t where t.name = ? and t.table_id <> ?";
		Object[] obj = new Object[] { model.getName(), model.getTable_id() };
		List<Sys_Lable> sllist = this.getList(hql, obj, 0, 0, "");
		if (sllist.size() <= 0) {
			Sys_Lable temp = (Sys_Lable) this.getT(new Sys_Lable(), model.getTable_id());
			temp.setJiaocai(model.getJiaocai());
			temp.setLableImportance(model.getLableImportance());
			temp.setLablePid(model.getLablePid());
			temp.setLableValue(model.getLableValue());
			temp.setName(model.getName());
			this.updateT(temp);

		} else {
			return 2;
		}
		return 1;
	}

	/**
	 * 增加一个标签，并返回这个标签
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Sys_Lable addSysLable1(Sys_Lable model) {
		// TODO Auto-generated method stub
		String hql = "from Sys_Lable t where t.name = ?";
		Object[] objec = new Object[] { model.getName() };
		List<Sys_Lable> sllist = this.getList(hql, objec, 0, 0, "");
		if (null == sllist || sllist.size() <= 0) {
			model.setLableId(UUID.randomUUID().toString());
			model.setCreateTime(Common.getTime1());
			model.setShowTime(Common.getTime());
			this.addT(model);
			// model = (Sys_Lable)this.getT(new Sys_Lable(),
			// model.getTable_id());
			return model;
		} else {
			return null;
		}
	}

	@Override
	public Map<String, Object> getAllLableTree(Sys_Lable model) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		// String opt =
		// "t.table_id, t.name, t.lableImportance, t.lableValue, t.type, t.jiaocai, t.lableId, t.lablePid, t.userName";
		// 这里把用户名放在string1里面的
		String opt = " t.table_id, t.name, t.lableImportance, t.lableValue, t.type, t.jiaocai, t.lableId, t.lablePid, t.sysUser.userName,t.sysUser.table_id,t.showTime ";
		String hsql = "from Sys_Lable t where t.type ='5' ";
		// String wheresql = "";
		// if (null != model.getType() && !"".equals(model.getType())) {
		// if (wheresql.length() > 3) {
		// wheresql += " and t.type ='" + model.getType() + "' ";
		// } else {
		// wheresql += " where t.type ='" + model.getType() + "' ";
		// }
		// }
		Object[] obj = new Object[] { model.getTable_id() };
		@SuppressWarnings("unchecked")
		List<SysUser> list1 = (List<SysUser>) this.getList("select new com.dream.model.Sys_Lable(" + opt + ") " + hsql, 0, 0, "");
		int count = this.getListCount("select count(*) " + hsql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	@Override
	public Sys_Lable addSysLable2(Sys_Lable model) {
		// TODO Auto-generated method stub
		String hql = "from Sys_Lable t where t.name = ?";
		Object[] objec = new Object[] { model.getName() };
		List<Sys_Lable> sllist = this.getList(hql, objec, 0, 0, "");
		if (null == sllist || sllist.size() <= 0) {
			model.setLableId(UUID.randomUUID().toString());
			model.setCreateTime(Common.getTime1());
			model.setShowTime(Common.getTime());
			this.addT(model);
			return model;
		} else {
			return null;
		}
	}

}
